﻿//===============================================================================
//	LoginServer.h
//	数据库存储服务器
//===============================================================================

#pragma once

#ifdef _MSC_VER
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif
#endif

#include "../share/server_net.h"
#include "../share/common_singleton.h"
#include "../share/server_base.h"
#include "../share/code_def.h"
#include "../share/proxy_msg_mgr.h"
#include "login_msg.h"
#include "../share/channel_base.h"
#include "../share/server_net_new.h"


class CLoginChannel : public common::net::CChannelBase
{
public:
	virtual void OnMsgEvent();

	virtual void OnConnect();

	virtual void OnDisconnect();
};

class LoginServer :
	public IService,
	public ::common::CSingleton<LoginServer>
{
public:
	LoginServer();
	~LoginServer();

public:
	int32_t Initialize(void);
	int32_t Finalize();

	int32_t client_msg_dispatch(std::string& msg, unsigned long uid, uint32_t msgId);
	bool is_connnected(uint32_t uid);
	// 从网络层往业务层抛出断开连接事件
	virtual void del_client_socket(uint32_t fd);
	// 主动断开连接，去往下调用断开连接事件
	virtual void client_close_socket(uint32_t fd);
	virtual EServiceType GetServiceType() { return LOGIN_SERVICE; }

	virtual void TickUpdate();
	virtual int32_t ServerStart();
public:
// 	virtual int32_t AddClientMsg(uint32_t socketId, uint32_t msgId, std::string &msg);
// 	virtual int32_t AddServerMsg(uint32_t msgId, std::string &msg, const common::CProxyHead& head);

	virtual void ProcessClientMsg(CClientSocketMsg& val);
public:
	void RegisteClientMsg(uint32_t msg_id, LoginPlayerMsgProcess fun)
	{
		login_client_msg_mgr_.RegisteMsgJson(msg_id, fun);
	}
	void RegisteServerMsg(uint32_t msg_id, ProxyMsgProcess func)
	{
		proxy_msg_mgr_.RegisteMsgJson(msg_id, func);
	}
	CLoginPlayerMsgFactory& GetPlayerMsgFactory() { return login_client_msg_mgr_; }
private:
	CServerNet server_net;

	CLoginPlayerMsgFactory login_client_msg_mgr_;

	common::net::CServerNet< CLoginChannel > login_server_;
};

#define registe_client_msg LoginServer::GetInstance().RegisteClientMsg
#define registe_server_msg LoginServer::GetInstance().RegisteServerMsg
